﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary
{
    /// <summary>
    /// This class provides methods to interpolate a linear function out of two plane points.
    /// </summary>
    [Serializable]
    public class LinearFunctionInterpolation
    {
        /// <summary>
        /// The first point for the interpolation.
        /// </summary>
        private Point2D firstPoint;

        /// <summary>
        /// The second point for the interpolation.
        /// </summary>
        private Point2D secondPoint;

        /// <summary>
        /// Initializes a new instance of the <see cref="LinearFunctionInterpolation"/> class.
        /// </summary>
        /// <param name="firstPoint">The first point for the interpolation.</param>
        /// <param name="secondPoint">The second point for the interpolation.</param>
        public LinearFunctionInterpolation(Point2D firstPoint, Point2D secondPoint)
        {
            if (firstPoint == (Point2D) null)
            {
                throw new ArgumentNullException("firstPoint");
            }

            if (secondPoint == (Point2D) null)
            {
                throw new ArgumentNullException("secondPoint");
            }

            this.firstPoint = firstPoint.Copy();
            this.secondPoint = secondPoint.Copy();
        }

        /// <summary>
        /// Gets or sets the first point for the interpolation.
        /// </summary>
        /// <value>The first point for the interpolation.</value>
        public Point2D FirstPoint
        {
            get { return firstPoint; }
            set { firstPoint = value; }
        }

        /// <summary>
        /// Gets or sets the second point for the interpolation.
        /// </summary>
        /// <value>The second point for the interpolation.</value>
        public Point2D SecondPoint
        {
            get { return secondPoint; }
            set { secondPoint = value; }
        }

        /// <summary>
        /// Interpolates this linear function out of the specified points.
        /// </summary>
        /// <returns>The interpolated linear function.</returns>
        public LinearFunction Interpolate()
        {
            if (this.firstPoint == this.secondPoint)
            {
                throw new ArgumentException("The specified points for the interpolation have to be different.");
            }

            double m = (this.secondPoint.Y - this.firstPoint.Y) / (this.secondPoint.X - this.firstPoint.X);
            double n = this.firstPoint.Y - (this.firstPoint.X * m);

            return new LinearFunction(m, n);
        }
    }
}